package com.zy.composeworld.ui.screens.image.image

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.BlurredEdgeTreatment
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.zy.composeworld.R


@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
fun ImagePager002() {

    LazyColumn(
        modifier = Modifier
            .padding(16.dp)
            .background(color = Color.Gray),
        verticalArrangement = Arrangement.spacedBy(16.dp),
    ) {

        val imageModifier = Modifier
            .size(150.dp)
            .border(BorderStroke(1.dp, Color.Black))
            .background(Color.Yellow)

        item {
            TextHead("ContentScale.Fit")
            Text(
                "均匀缩放图片，并保持宽高比（默认）。如果内容小于指定大小，系统会放大图片以适应边界",
                fontSize = 12.sp
            )
            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Fit,
                    modifier = imageModifier
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Fit,
                    modifier = imageModifier
                )
            }


        }

        item {
            TextHead("ContentScale.Crop")
            Text(
                "将图片居中裁剪到可用空间",
                fontSize = 12.sp
            )

            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = imageModifier
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = imageModifier
                )
            }

        }

        item {
            TextHead("ContentScale.FillHeight")
            Text(
                "缩放来源图片，保持宽高比不变，使边界与目标高度匹配",
                fontSize = 12.sp
            )

            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.FillHeight,
                    modifier = imageModifier
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.FillHeight,
                    modifier = imageModifier
                )
            }

        }


        item {
            TextHead("ContentScale.FillWidth")
            Text(
                "缩放来源图片，保持宽高比不变，使边界与目标宽度匹配",
                fontSize = 12.sp
            )

            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.FillWidth,
                    modifier = imageModifier
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.FillWidth,
                    modifier = imageModifier
                )
            }

        }

        item {
            TextHead("ContentScale.FillBounds")
            Text(
                "以非均匀方式垂直和水平缩放内容，以填充目标边界。（注意：如果将图片放入与其宽高比不完全相符的容器中，则图片会失真）",
                fontSize = 12.sp
            )

            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.FillBounds,
                    modifier = imageModifier
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.FillBounds,
                    modifier = imageModifier
                )
            }

        }

        item {
            TextHead("ContentScale.Inside")
            Text(
                "缩放来源图片，使宽高保持在目标边界内。如果来源图片在两个维度上都小于或等于目标，则其行为类似于“None”。内容始终包含在边界内。如果内容小于边界，则不会应用缩放",
                fontSize = 12.sp
            )

            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Inside,
                    modifier = imageModifier
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Inside,
                    modifier = imageModifier
                )
            }

        }


        item {
            TextHead("ContentScale.None")
            Text(
                "不对来源图片应用任何缩放。如果内容小于目标边界，则不会缩放以适应相应区域",
                fontSize = 12.sp
            )

            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.None,
                    modifier = imageModifier
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.None,
                    modifier = imageModifier
                )
            }

        }

        item {
            TextHead("裁剪图片")


            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(100.dp)
                        .clip(CircleShape)
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(100.dp)
                        .clip(CircleShape)
                )
            }

            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(100.dp)
                        .clip(RoundedCornerShape(8.dp))
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(100.dp)
                        .clip(RoundedCornerShape(16.dp))
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(100.dp)
                        .clip(RoundedCornerShape(32.dp))
                )
            }

            TextHead("自定义")

            Row {


                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(100.dp)
                        .clip(SquashedOval())
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(100.dp)
                        .clip(SquashedOval())
                )
            }

        }

        item {
            TextHead("为 Image 可组合项添加边框")
            val borderWidth = 4.dp

            Row {

                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(150.dp)
                        .border(
                            BorderStroke(borderWidth, Color.Yellow),
                            CircleShape
                        )
                        .padding(borderWidth)
                        .clip(CircleShape)
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(150.dp)
                        .border(
                            BorderStroke(borderWidth, Color.Blue),
                            CircleShape
                        )
                        .padding(borderWidth)
                        .clip(CircleShape)
                )
            }

            val rainbowColorsBrush = remember {
                Brush.sweepGradient(
                    listOf(
                        Color(0xFF9575CD),
                        Color(0xFFBA68C8),
                        Color(0xFFE57373),
                        Color(0xFFFFB74D),
                        Color(0xFFFFF176),
                        Color(0xFFAED581),
                        Color(0xFF4DD0E1),
                        Color(0xFF9575CD)
                    )
                )
            }

            TextHead("渐变边框")
            Row {

                Image(
                    painter = painterResource(id = R.drawable.ic_dog1),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(150.dp)
                        .border(
                            BorderStroke(borderWidth, rainbowColorsBrush),
                            CircleShape
                        )
                        .padding(borderWidth)
                        .clip(CircleShape)
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_dog2),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(150.dp)
                        .border(
                            BorderStroke(borderWidth, rainbowColorsBrush),
                            CircleShape
                        )
                        .padding(borderWidth)
                        .clip(CircleShape)
                )
            }

        }


        item {
            TextHead("设置自定义宽高比")

            Text(text = "16/9")
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier.aspectRatio(16f / 9f)
            )

            Text(text = "9/16")
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier.aspectRatio(9f / 16f)
            )

            Text(text = "1/1")
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier.aspectRatio(1f / 1f)
            )

        }


        item {
            TextHead("颜色滤镜 - 转换图片的像素颜色")

            Text(text = "对图片进行色调调节")
            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_like_selected),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_like_selected),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    colorFilter = ColorFilter.tint(Color.Yellow),
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_like_selected),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    colorFilter = ColorFilter.tint(Color.Blue),
                )
                Image(
                    painter = painterResource(id = R.drawable.ic_like_selected),
                    contentDescription = stringResource(id = R.string.dog_content_description),
                    contentScale = ContentScale.Crop,
                    colorFilter = ColorFilter.tint(Color.Green),
                )
            }


            val borderWidth = 4.dp
            Text(text = "BlendMode.Darken")
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .border(
                        BorderStroke(borderWidth, Color.Yellow),
                        CircleShape
                    )
                    .padding(borderWidth)
                    .clip(CircleShape),
                colorFilter = ColorFilter.tint(Color.Green, blendMode = BlendMode.Darken)
            )

            Text(text = "BlendMode.Overlay")
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .border(
                        BorderStroke(borderWidth, Color.Yellow),
                        CircleShape
                    )
                    .padding(borderWidth)
                    .clip(CircleShape),
                colorFilter = ColorFilter.tint(Color.Green, blendMode = BlendMode.Overlay)
            )


            Text(text = "BlendMode.DstIn")
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .border(
                        BorderStroke(borderWidth, Color.Yellow),
                        CircleShape
                    )
                    .padding(borderWidth)
                    .clip(CircleShape),
                colorFilter = ColorFilter.tint(Color.Green, blendMode = BlendMode.DstIn)
            )

        }

        item {
            TextHead("通过颜色矩阵应用 Image 滤镜")


            val borderWidth = 4.dp
            Text(text = "饱和度为 0 的颜色矩阵（黑白图片）")
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .border(
                        BorderStroke(borderWidth, Color.Yellow),
                        CircleShape
                    )
                    .padding(borderWidth)
                    .clip(CircleShape),
                colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply {
                    setToSaturation(0f)
                })
            )

            Text(text = "调整 Image 可组合项的对比度或亮度")

            val contrast = 2f // 0f..10f (1 should be default)
            val brightness = -180f // -255f..255f (0 should be default)
            val colorMatrix = floatArrayOf(
                contrast, 0f, 0f, 0f, brightness,
                0f, contrast, 0f, 0f, brightness,
                0f, 0f, contrast, 0f, brightness,
                0f, 0f, 0f, 1f, 0f
            )
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .border(
                        BorderStroke(borderWidth, Color.Yellow),
                        CircleShape
                    )
                    .padding(borderWidth)
                    .clip(CircleShape),
                colorFilter = ColorFilter.colorMatrix(ColorMatrix(colorMatrix))
            )
            Text(text = "反转 Image 可组合项的颜色")

            val colorMatrix1 = floatArrayOf(
                -1f, 0f, 0f, 0f, 255f,
                0f, -1f, 0f, 0f, 255f,
                0f, 0f, -1f, 0f, 255f,
                0f, 0f, 0f, 1f, 0f
            )
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .border(
                        BorderStroke(borderWidth, Color.Yellow),
                        CircleShape
                    )
                    .padding(borderWidth)
                    .clip(CircleShape),
                colorFilter = ColorFilter.colorMatrix(ColorMatrix(colorMatrix1))
            )
        }

        item {
            TextHead("对 Image 可组合项进行模糊处理")
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .blur(
                        radiusX = 10.dp,
                        radiusY = 10.dp,
                        edgeTreatment = BlurredEdgeTreatment(RoundedCornerShape(1.dp))
                    )
            )

            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .blur(
                        radiusX = 10.dp,
                        radiusY = 10.dp,
                        edgeTreatment = BlurredEdgeTreatment(RoundedCornerShape(2.dp))
                    )
            )

            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .blur(
                        radiusX = 10.dp,
                        radiusY = 10.dp,
                        edgeTreatment = BlurredEdgeTreatment(RoundedCornerShape(4.dp))
                    )
            )

            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .blur(
                        radiusX = 10.dp,
                        radiusY = 10.dp,
                        edgeTreatment = BlurredEdgeTreatment(RoundedCornerShape(8.dp))
                    )
            )
            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .blur(
                        radiusX = 10.dp,
                        radiusY = 10.dp,
                        edgeTreatment = BlurredEdgeTreatment(RoundedCornerShape(16.dp))
                    )
            )

            Image(
                painter = painterResource(id = R.drawable.ic_dog1),
                contentDescription = stringResource(id = R.string.dog_content_description),
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(150.dp)
                    .blur(
                        radiusX = 10.dp,
                        radiusY = 10.dp,
                        edgeTreatment = BlurredEdgeTreatment.Unbounded
                    )
                    .clip(RoundedCornerShape(8.dp))
            )
        }


    }

}

class SquashedOval : Shape {
    override fun createOutline(
        size: Size,
        layoutDirection: LayoutDirection,
        density: Density
    ): Outline {
        val path = Path().apply {
            // We create an Oval that starts at ¼ of the width, and ends at ¾ of the width of the container.
            addOval(
                Rect(
                    left = size.width / 4f,
                    top = 0f,
                    right = size.width * 3 / 4f,
                    bottom = size.height
                )
            )
        }
        return Outline.Generic(path = path)
    }
}


@Composable
private fun TextHead(text: String) {
    Text(text, fontWeight = FontWeight.Bold, color = Color.Red)
}